/*
 * $Id: HelloWorld.template,v 1.2 2008-03-27 05:47:21 ub3rsold4t Exp $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Map;
import thesis.haipv.qa.model.Answer;
import thesis.haipv.qa.model.Question;
import thesis.haipv.qa.model.UserProfile;
import thesis.haipv.qa.persistence.AnswerDAO;
import thesis.haipv.qa.persistence.ExperienceTrustDAO;
import thesis.haipv.qa.persistence.QuestionDAO;
import thesis.haipv.qa.persistence.UserProfileDAO;

/**
 * <code>Set welcome message.</code>
 */
public class Controller extends ActionSupport {

    private Paging paging;
    
    private ArrayList<Question> questions;
    private Question question;
    private String answer;
    private String q, t, a, u, page;

    @Override
    public String execute() throws Exception {
        
        QuestionDAO dao = new QuestionDAO();
        int _page = (page != null) ? (Integer.parseInt(page)) : 1;
        paging = new Paging(_page);
        questions = dao.getLastQuestions(_page);
        if(questions.isEmpty()) {
            paging.setIsNext(false);
        }
        return SUCCESS;
    }

    public String viewQuestion() throws Exception {
        QuestionDAO dao = new QuestionDAO();
        Map session = ActionContext.getContext().getSession();
        UserProfile user = (UserProfile) session.get("user");
        ExperienceTrustDAO exdao = new ExperienceTrustDAO();

        if (user == null) {
            question = dao.getQuestionById(q, null);
            for (Answer a : question.getAnswers()) {
                a.getResponder().setExperienceTrust(-1);
            }
        } else {
            question = dao.getQuestionById(q, user.getAccountId());
            for (Answer a : question.getAnswers()) {
                a.getResponder().setExperienceTrust(exdao.getExperienceTrust(user.getAccountId(), a.getResponder().getAccountId()));
            }
        }

        return SUCCESS;
    }

    public String postAnswer() throws Exception {

        long createtime = System.currentTimeMillis();
        Map session = ActionContext.getContext().getSession();
        UserProfile user = (UserProfile) session.get("user");
        UserProfileDAO dao = new UserProfileDAO();
        //dao.postAnswer(user.getAccountId(), answer, Integer.parseInt(q), createtime);
        return SUCCESS;
    }

    public ArrayList<Question> getQuestions() {
        return questions;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQ(String q) {
        this.q = q;
    }

    public String getQ() {
        return q;
    }

    public void setT(String t) {
        this.t = t;
    }

    public void setA(String a) {
        this.a = a;
    }

    public void setU(String u) {
        this.u = u;
    }
    
    public void setPage(String page) {
        this.page = page;
    }

    public int getPage() {
        return Integer.parseInt(page);
    }
    

    public Paging getPaging() {
        return paging;
    }

    
    
    public void setAnswer(String answer) {
        this.answer = answer;
    }
}
